<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="链接名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="链接名称"></el-input>
    </el-form-item>
    <!-- <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="驱动名" prop="driverName">
      <el-input v-model="dataForm.driverName" placeholder="驱动名"></el-input>
    </el-form-item> -->


    <el-form-item label="数据库" >

     <el-select v-model="selectedDataBase"  @change="dataBaseOnChanged($event)" collapse-tags style="margin-left: 20px;" placeholder="请选择">
        <el-option
          v-for="item in databaseList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      </el-form-item>


    <el-form-item label="数据库名" prop="databaseName">
      <el-input v-model="dataForm.databaseName" placeholder="数据库名"></el-input>
    </el-form-item>
    <el-form-item label="主机ip" prop="ip">
      <el-input v-model="dataForm.ip" placeholder="主机ip"></el-input>
    </el-form-item>
    <el-form-item label="端口" prop="port">
      <el-input v-model="dataForm.port" placeholder="端口"></el-input>
    </el-form-item>
    <el-form-item label="数据库用户名" prop="connectUsername">
      <el-input v-model="dataForm.connectUsername" placeholder="数据库用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="connectPassword">
      <el-input v-model="dataForm.connectPassword" placeholder="密码"></el-input>
    </el-form-item>
    <!-- <el-form-item label="数据库链接的url" prop="connectUrl">
      <el-input v-model="dataForm.connectUrl" placeholder="数据库链接的url"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="编码方式" prop="encode">
      <el-input v-model="dataForm.encode" placeholder="编码方式"></el-input>
    </el-form-item> -->

      <el-form-item label="分配用户" >

     <el-select v-model="selectedUser" multiple @change="userOnChanged($event)" collapse-tags style="margin-left: 20px;" placeholder="请选择">
        <el-option
          v-for="item in userList"
          :key="item.value"
          :label="item.label"
         
          :value="item.value">
        </el-option>
      </el-select>
      </el-form-item>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>



<script>
  export default {
    data () {
      return {
        databaseList:[
           {
             value: '1',
             label: 'mysql'
           },
           {
             value: '2',
             label: 'oracle'
           }
        ],
        selectedDataBase:"1",

        userList: [],
        selectedUser: [],
        value2: [],

        visible: false,
        dataForm: {
          id: 0,
          name: '',
        //  driverName: '',
          databaseName: '',
          ip: '',
          port: '',
          createName: '',
          connectPassword: '',
          connectUsername: ''
       //   connectUrl: '',
         // encode: ''
        },
        dataRule: {
          name: [
            { required: true, message: '数据库链接名不能为空', trigger: 'blur' }
          ],
          // driverName: [
          //   { required: true, message: '驱动名不能为空', trigger: 'blur' }
          // ],
          databaseName: [
            { required: true, message: '数据库名不能为空', trigger: 'blur' }
          ],
          ip: [
            { required: true, message: '主机ip不能为空', trigger: 'blur' }
          ],
          port: [
            { required: true, message: '端口不能为空', trigger: 'blur' }
          ],
        
          connectPassword: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],

                    connectUsername: [
            { required: true, message: '数据库链接名不能为空', trigger: 'blur' }
          ]
          // connectUrl: [
          //   { required: true, message: '数据库链接的url不能为空', trigger: 'blur' }
          // ],
          // encode: [
          //   { required: true, message: '编码方式不能为空', trigger: 'blur' }
          // ]
        }
      }
    },

    methods: {

      dataBaseOnChanged(e){
        this.selectedDataBase=e;
        
      },

      userOnChanged(e){
        this.selectedUser=e;
      },

      getUsers(connectId){
        this.$http({
          url: this.$http.adornUrl('/report/connection/users'),
          method: 'get',
          params: this.$http.adornParams(
            {
             'connectId': connectId
           }
          )
        }).then(({data}) => {

              for(var i=0;i<data.userList.length;i++){
                 this.selectedUser.push(data.userList[i].value);
              }

        })
      },

      getAllUsers(){
        this.$http({
          url: this.$http.adornUrl('/report/connection/users'),
          method: 'get',
          params: this.$http.adornParams(
          )
        }).then(({data}) => {
              this.userList=data.userList;
        })
      },


      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.getAllUsers();


          if (this.dataForm.id) {
            this.getUsers(this.dataForm.id);
            this.$http({
              url: this.$http.adornUrl(`/report/connection/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.connection.name
                this.dataForm.createDate = data.connection.createDate
               // this.dataForm.driverName = data.connection.driverName
                this.dataForm.databaseName = data.connection.databaseName
                this.dataForm.ip = data.connection.ip
                this.dataForm.port = data.connection.port
                this.dataForm.connectUsername = data.connection.connectUsername
                this.dataForm.connectPassword = data.connection.connectPassword
              //  this.dataForm.connectUrl = data.connection.connectUrl
               // this.dataForm.encode = data.connection.encode
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/report/connection/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
        'name': this.dataForm.name,
                'createDate': this.dataForm.createDate,
                'driverName': this.selectedDataBase,
                'databaseName': this.dataForm.databaseName,
                'ip': this.dataForm.ip,
                'port': this.dataForm.port,
                'createName': this.$store.state.user.name,
                'connectPassword': this.dataForm.connectPassword,
                      data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'createDate': this.dataForm.createDate,
                'driverName': this.selectedDataBase,
                'databaseName': this.dataForm.databaseName,
                'ip': this.dataForm.ip,
                'port': this.dataForm.port,
                'createName': this.$store.state.user.name,
                'connectPassword': this.dataForm.connectPassword,
                'connectUsername': this.dataForm.connectUsername,
                'userIds': this.selectedUser
               // 'connectUrl': this.dataForm.connectUrl,
                //'encode': this.dataForm.encode
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
